package org.backend;

import org.backend.annotation.NotNull;

import java.lang.reflect.Field;

/**
 * 注解处理器，用于验证对象的必填字段
 */
public class AnnotationProcessor {

    public static void validate(Object obj) throws IllegalAccessException {
        if (obj == null) {
            throw new IllegalArgumentException("对象不能为空");
        }

        Class<?> clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();

        for (Field field : fields) {
            if (field.isAnnotationPresent(NotNull.class)) {
                field.setAccessible(true);
                Object value = field.get(obj);
                if (value == null || (value instanceof String && ((String) value).trim().isEmpty())) {
                    NotNull annotation = field.getAnnotation(NotNull.class);
                    throw new IllegalArgumentException(annotation.message());
                }
            }
        }
    }
}

